using System;
using Microsoft.SPOT;
using WebServer;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using NetMf.CommonExtensions;

    public class GetFile
    {
        public void Action(Request request, SDCard sdCard)
        {
            Debug.Print("GetFile Command received:" + request.Uri);
            // send start
            string header = HttpGeneral.GetHttpHeader(-1);// no length
 
            sdCard.ReadInChunks("", new BytesDelegate(SendToSocket));


        }

        public void SendToSocket(byte[] data)
        {

        }


        ///// <summary>
        ///// Sends a file back to the client
        ///// </summary>
        ///// <remarks>
        ///// Assumes the application using this has checked whether it exists
        ///// </remarks>
        ///// <param name="filePath"></param>
        //public void SendFile(string filePath)
        //{
        //    // Map the file extension to a mime type
        //    string type = "";
        //    int dot = filePath.LastIndexOf('.');
        //    if (dot != 0)
        //        switch (filePath.Substring(dot + 1))
        //        {
        //            case "css":
        //                type = "text/css";
        //                break;
        //            case "xml":
        //            case "xsl":
        //                type = "text/xml";
        //                break;
        //            case "jpg":
        //            case "jpeg":
        //                type = "image/jpeg";
        //                break;
        //            case "gif":
        //                type = "image/gif";
        //                break;
        //            // Not exhaustive. Extend this list as required.
        //        }


        //    using (FileStream inputStream = new FileStream(filePath, FileMode.Open))
        //    {
        //        // Send the header
        //        string header = "HTTP/1.0 200 OK\r\nContent-Type: " + type + "; charset=utf-8\r\nContent-Length: " + inputStream.Length.ToString() + "\r\nConnection: close\r\n\r\n";
        //        client.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);

        //        byte[] readBuffer = new byte[fileBufferSize];
        //        while (true)
        //        {
        //            // Send the file a few bytes at a time
        //            int bytesRead = inputStream.Read(readBuffer, 0, readBuffer.Length);
        //            if (bytesRead == 0)
        //                break;

        //            client.Send(readBuffer, bytesRead, SocketFlags.None);
        //            Debug.Print("Sending " + readBuffer.Length.ToString() + "bytes...");
        //        }
        //    }

        //    Debug.Print("Sent file " + filePath);
        //}
    }

